home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / cug106 / strings.c < prev    next >
Text File  |  1984-06-14  |  11KB  |  479 lines

  1. /*    strings.c
  2.  
  3.     Version 2.1    15-Apr-80
  4.  
  5.       this set of string functions manipulates character
  6.     arrays in a variety of useful ways. note that positions
  7.     are offset 1 from the internal workings of 'C'. Thus
  8.     the first position in a string is 1 which corresponds
  9.     to array position [0].
  10.  
  11.     NOTE: none of these routines alter the original string. 
  12.           therefore, to assign the altered version of the
  13.           string to the original string, use:
  14.  
  15.            strcpy(str,funct(s,..));
  16.                (or c = match(str,"xxx",x); in the case of match)
  17.  
  18.           since the result is stored in a work buffer (strbuf),
  19.           it should be transferred immediately to avoid being
  20.           clobbered. therefore, complex statements involving
  21.           more than one of these functions are illegal and must
  22.           be broken into steps.
  23.  
  24.     IMPORTANT: in order to communicate with the external
  25.            work buffer, all separately compiled files
  26.            should contain the following declarations
  27.            as the first order of business.
  28.  
  29.            char strbuf[256];
  30.            #define SBP    &strbuf[0]
  31.  
  32.            failure to do this will result in garbage
  33.            results from independent functions which
  34.            must refer to this common memory block.
  35.            NOTE: files created prior to 04-Apr-80
  36.            use 'work' instead of 'strbuf' - change to
  37.            'strbuf' before recompiling (and WP to SBP).
  38.  
  39.         Function Summary
  40.         ----------------
  41.  
  42. 1. delete(string,n,fpos)
  43.  
  44.     delete n characters from a copy of the given string
  45.     beginning at the first position (fpos). return the
  46.     altered string.
  47.  
  48. 2. insert(string,istring,fpos)
  49.  
  50.     insert the insertion string (istring) into a copy of
  51.     the given string beginning at the first position (fpos).
  52.     return the altered string.
  53.  
  54. 3. left(string,n)
  55.  
  56.     return the leftmost n characters from a copy of the
  57.     given string.
  58.  
  59. 4. mask(string,mstring)
  60.  
  61.     return a subset of a copy of the given string according
  62.     to the following rules:
  63.  
  64.          i) if the mask character is '?' the character is
  65.             returned.
  66.         ii) if the mask character is 'x' the character is
  67.             deleted.
  68.        iii) if the mask character is ' ' the character is
  69.             replaced by a space.
  70.  
  71.     example:
  72.  
  73.     #define TESTMASK "???  ?xx?xxx";
  74.     mask("ABCDEFGHIJKL",TESTMASK) = "ABC  FI"
  75.  
  76. 5. match(string,cstring,fpos)
  77.  
  78.     return the first position of a match of the comparison
  79.     string (cstring) within the string beginning at the first
  80.     position (fpos). return NONE (-1) if there is no match.
  81.  
  82. 6. mid(string,fpos,n)
  83.  
  84.     return a subset of n characters beginning at the first
  85.     position (fpos) of a copy of the given string.
  86.  
  87. 7. replace(string,rstring,fpos)
  88.  
  89.     replace (an equal number of) characters in a copy of the
  90.     given string with the replacement string (rstring) beginning
  91.     at the first position (fpos). return the altered string.
  92.  
  93. 8. reverse(string)
  94.  
  95.     return a copy of the given string with the characters reversed.
  96.  
  97. 9. right(string,n)
  98.  
  99.     return the rightmost n characters of a copy of the
  100.     given string.
  101. */
  102.  
  103. char strbuf[256];
  104.  
  105. #define SBP    &strbuf[0]    /* fwa of work buffer */
  106. #define EOS    '\0'        /* string terminator  */
  107. #define NONE    -1        /* negative indicator */
  108.  
  109. delete(s,n,fpos)
  110. int n, fpos;
  111. char *s;
  112. {
  113.     char *tp; tp = SBP;
  114.  
  115.     while (--fpos != 0) *tp++ = *s++; s += n;
  116.     while (*tp++ = *s++); return SBP;
  117. }
  118.  
  119. insert(s,is,fpos)
  120. int fpos;
  121. char *s, *is;
  122. {
  123.     char *tp; tp = SBP;
  124.  
  125.     while (--fpos != 0) *tp++ = *s++;
  126.     while (*tp++ = *is++); --tp;
  127.     while (*tp++ = *s++); return SBP;
  128. }
  129.     
  130. left(s,n)
  131. int n;
  132. char *s;
  133. {
  134.     char *tp; tp = SBP;
  135.  
  136.     while (n-- != 0) *tp++ = *s++;
  137.     *tp = EOS; return SBP;
  138. }
  139.  
  140. mask(s,ms)
  141. char *s, *ms;
  142. {
  143.     int n;
  144.     char *tp; tp = SBP;
  145.  
  146.     n = strlen(s);
  147.     while (n-- != 0) {
  148.     switch(*ms) {
  149.        case '?': *tp = *s;  break;
  150.        case ' ': *tp = ' '; break;
  151.        case 'x': --tp;      break;
  152.     }
  153.     tp++; s++; ms++;
  154.     }
  155.     *tp = EOS; return SBP;
  156. }
  157.       
  158. match(s,cs,fpos)
  159. char s[], cs[];
  160. int fpos;
  161. {
  162.     int i, j, k;
  163.  
  164.     for (i=--fpos; s[i] != EOS; i++) {
  165.         for (j=i, k=0; cs[k] != EOS && s[j] == cs[k]; j++, k++)
  166.             ;
  167.         if (cs[k] == EOS) return(++i);
  168.     }
  169.     return(NONE);
  170. }
  171.  
  172. mid(s,fpos,n)
  173. int fpos, n;
  174. char *s;
  175. {
  176.     char *tp; tp = SBP;
  177.  
  178.     s += --fpos;
  179.     while (n-- != 0) *tp++ = *s++;
  180.     *tp = EOS; return SBP;
  181. }
  182.  
  183. replace(s,rs,fpos)
  184. int fpos;
  185. char *s, *rs;
  186. {
  187.     int n;
  188.     char *tp; tp = SBP;
  189.  
  190.     n = strlen(rs);
  191.     while (--fpos != 0) *tp++ = *s++; s += n;
  192.     while (*tp++ = *rs++); --tp;
  193.     while (*tp++ = *s++); return SBP;
  194. }
  195.  
  196. reverse(s)
  197. char *s;
  198. {
  199.     int n;
  200.     char *tp; tp = SBP;
  201.  
  202.     n = strlen(s); s += n-1;
  203.     while (n-- != 0) *tp++ = *s--;
  204.     *tp = EOS; return SBP;
  205. }
  206.  
  207. right(s,n)
  208. int n;
  209. char *s;
  210. {
  211.     char *tp; tp = SBP;
  212.  
  213.     s += strlen(s)-n;
  214.     while (*tp++ = *s++); return SBP;
  215. }
  216. s[i] - t[i];
  217. }
  218.  
  219.  
  220. strcpy(s1,s2)
  221. char *s1, *s2;
  222. {
  223.     char *temp; temp=s1;
  224.     while (*s1++ = *s2++);
  225.     return temp;
  226. }
  227.  
  228.  
  229. strlen(s)
  230. char *s;
  231. {
  232.     int len;
  233.     len=0;
  234.     while (*s++) len++;
  235.     return(len);
  236. }
  237.  
  238.  
  239. /*
  240.     Some character diddling functions
  241. *//*    standard definition library file
  242. **
  243. **    Version 1.3    16-Aug-80
  244. */
  245.  
  246. char               strbuf[256];        /* string buffer */
  247. #define SBP         &strbuf[0]
  248.  
  249. #define    BDOS_ERR    255
  250. #define    CPM_EOF        26
  251. #define    DIGIT        0
  252. #define    EOF        -1
  253. #define    EQUAL        0
  254. #define    ERROR        -1
  255. #define    FALSE        0
  256. #define    K        1024
  257. #define    NO         0
  258. #define    RESET        0
  259. #define    SECTOR      128
  260. #define    SET        -1
  261. #define TRUE        -1
  262. #define    U_CASE        95
  263. #define    YES        1
  264.  
  265. #define    ALPHA         'a'
  266. #define    ANY         '?'
  267. #define    ASCII         48
  268. #define    BELL         7
  269. #define    CR         '\r'
  270. #define    D_QUOTE         '"'
  271. #define    EOS         '\0'
  272. #define    ESCAPE         27
  273. #define    FORM_FEED     12
  274. #define    LF         '\n'    
  275. #define    NEWLINE         '\n'
  276. #define NULL        '\0'
  277. #define    QUOTE         39
  278. #define    SPACE         ' '
  279. #define    TAB         '\t'
  280. ns are illegal and must
  281.           be broken into steps.
  282.  
  283.     IMPORTANT: in order to communicate with the external
  284.            work buffer, all separately compiled files
  285.            should contain the following declarations
  286.            as the first order of business.
  287.  
  288.            ch;                strfun.asm
  289. ;
  290. ;    Copyright (C) 1980, M J Maney
  291. ;
  292. ;    First created 8/25/80
  293. ;    Last revised 8/30/80 19:15
  294. ;
  295. ;    Herein are the basic string functions that C people like to use,
  296. ;    coded in assembler to make them as fast as can be.
  297. ;
  298. ;    NOTE that I have used .NE. in the comments in place of the usual C
  299. ;    symbol, because MAC sometimes objects to such usage.
  300. ;
  301.     maclib    crl
  302. ;
  303. ;
  304. ; * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  305. ;
  306. ;                string functions
  307. ;
  308. ;    strlen        strcmp        strcpy        strcat
  309. ;
  310. ; * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  311. ;
  312. ;
  313. ;    strlen(str)
  314. ;    char *str;
  315. ;
  316. ;    Returns the length of the string pointed to by str. It is assumed
  317. ;    that the string is terminated by '\0', and the terminator is NOT
  318. ;    counted as a character, so the storage length of a string s is
  319. ;    strlen(s) + 1.
  320. ;
  321.     PROC    STRLEN
  322.     lxi    d,0
  323.     lhld    ARG1        ;str to HL
  324.     mvi    a,0
  325. strlen1    cmp    m
  326.     BZ    strlen2        ;for (i=0; *str++ .NE. EOS; i++) ;
  327.     inx    h
  328.     inx    d
  329.     BRA    strlen1
  330. ;
  331. strlen2    xchg            ;return i;
  332.     ret
  333.     PEND    STRLEN
  334. ;
  335. ;
  336. ;    strcmp(sx,sy)
  337. ;    char *sx,*sy;
  338. ;
  339. ;    Returns 1, 0, or -1, depending upon the lexicographical relation
  340. ;    of the strings pointed to by sx and sy. The ordering is essentially
  341. ;    that which would be used if the strings were words being alphebetized
  342. ;    except that upper case and lower case are distinct, and digits and
  343. ;    punctuation are treated as significant characters. The relation
  344. ;    between any two characters is simply the relation between the bit
  345. ;    patterns that are used to represent them, treated as unsigned
  346. ;    integers. It is also assumed that the string terminator, '\0', has
  347. ;    a smaller value than any other character (in fact, its zero).
  348. ;
  349. ;    1 if sx > sy, 0 if sx == sy, or -1 if sx < sy
  350. ;
  351.     PROC    STRCMP
  352.     lhld    ARG1
  353.     xchg            ;sx to DE
  354.     lhld    ARG2        ;sy to HL
  355. ;
  356. strcmp1    ldax    d
  357.     cmp    m        ;while (*sy == (c = *sx)) {
  358.     BNZ    strcmp2
  359.     inx    h        ;    sy++;
  360.     inx    d        ;    sx++;
  361.     ora    a
  362.     BNZ    strcmp1        ;    if (c == EOS)
  363.     lxi    h,0        ;        return 0;
  364.     ret            ;    }
  365. strcmp2    lxi    h,1        ;if (*sx > *sy)
  366.     rnc            ;    return 1;
  367.     dcx    h        ;else
  368.     dcx    h
  369.     ret            ;    return -1;
  370.     PEND    STRCMP
  371. ;
  372. ;
  373. ;    strcpy(des,src)
  374. ;    char *des,*src;
  375. ;
  376. ;    Copies string pointed to by src into memory starting at des. No
  377. ;    provision is made for limiting the amount of stuff copied: it is
  378. ;    the user's responsibility to insure that the string being copied
  379. ;    will fit where it is being copied to.
  380. ;
  381.     PROC    STRCPY
  382.     lhld    ARG1
  383.     xchg            ;des to DE
  384.     lhld    ARG2        ;src to HL
  385. strcpy1    mov    a,m        ;do {
  386.     inx    h
  387.     stax    d
  388.     inx    d        ;    *des++ = c = *src++;
  389.     ora    a
  390.     BNZ    strcpy1        ;    }while (c .NE. EOS);
  391.     ret            ;return;
  392.     PEND    STRCPY
  393. ;
  394. ;
  395. ;    strcat(des,src)
  396. ;    char *des,*src;
  397. ;
  398. ;    Copies the string pointed to by src onto the tail end of the string
  399. ;    pointed to by des. This is equivalent to
  400. ;
  401. ;        strcpy(des+strlen(des),src)
  402. ;
  403. ;    and there is an identical lack of testing for overrun during the
  404. ;    copying process.
  405. ;
  406.     PROC    STRCAT
  407.     lhld    ARG2
  408.     xchg            ;src to DE
  409.     lhld    ARG1        ;des to HL
  410.     sub    a
  411. strcat1    cmp    m        ;while (*des++ .NE. EOS)
  412.     inx    h        ;    ;
  413.     BNZ    strcat1
  414.     dcx    h        ;des--;
  415. strcat2    ldax    d        ;do {
  416.     inx    d
  417.     mov    m,a
  418.     inx    h        ;    *des++ = c = *src++;
  419.     ora    a
  420.     BNZ    strcat2        ;    }while (c .NE. EOS);
  421.     ret            ;return;
  422.     PEND    STRCAT
  423. ;
  424. ;
  425.     end
  426. P;
  427. }
  428.  
  429. insert(s,is,fpos)
  430. int fpos;
  431. char *s, *is;
  432. {
  433.     char *tp; tp = SBP;
  434.  
  435.     while (--fpos != 0) *tp++ = *s++;
  436.     while (*tp++ = *is++); --tp;
  437.     while (*tp++ = *s++); return SBP;
  438. }
  439.     
  440. left(s,n)
  441. int n;
  442. char *s;
  443. {
  444.     char *tp; tp = SBP;
  445.  
  446.     while (n-/*                stdio.src
  447.  
  448.     Copyright (C) 1980, M J Maney
  449.  
  450.     First created    3/15/80
  451.     Last revised    4/19/80 1:45
  452.  
  453.     This file defines some useful functions that are designed to help
  454.     make BDS C programs running under CP/M pretend that they're
  455.     running in a nicer environment (like UNIX, ha-ha). It makes it
  456.     easier to setup and use STDIN and STDOUT directed IO, and
  457.     although you can't just assume that they're there as you can
  458.     with UNIX C, at least you don't have to put in code to parse
  459.     the command line into each program you write: just use the
  460.     functions here.
  461. */
  462.  
  463.  
  464. #include "csym.lib"
  465. #include "stdio.lib"
  466.  
  467. #define ESCAPE '\\'
  468.  
  469.  
  470. /* stdopen parses the command line arguments looking for arguments that
  471.     begin with "<" or ">", which are respectively taken to be the
  472.     filenames for STDIN and STDOUT. The defaults are to use the
  473.     console for both STDIN and STDOUT. Any argument beginning with
  474.     "<" or ">" is effectively removed from the argument list (by
  475.     moving pointers in argv[] and decreasing argc)...if your program
  476.     wants to get an argument beginning with "<" or ">", you must
  477.     type it as "\<" or "\>", and stdopen will replace the escape
  478.     sequence in the strings, but will not "use" those arguments
  479.     for IO assignment. If you want to pass arguments beg